/**
 * Created by Administrator on 2017/2/19 0019.
 */
/**
 * Created by Administrator on 2017/2/19 0019.
 */
/*
 此文件里的js专门用于封装jq 操作dom的方法,
 在引用之前必须先引入myJq.js文件
 */

var domOperate = {
    appendTo: function (addNOde, objNode) {
        objNode.appendChild(addNOde);
    },
    prependTo: function (addNode, objNode) {
        if (objNode.childNodes) {
            objNode.insertBefore(addNode, objNode.firstChild)
        }
    },
    insertBefore: function (addNode, objNode) {
        objNode.parentNode.insertBefore(addNode, objNode)
    },
    insertAfter: function (addNode, objNode) {
        if (objNode.nextSibling) {
            objNode.parentNode.insertBefore(addNode, obj.nextElementSibling)
        } else {
            objNode.parentNode.appendChild(addNode);
        }
    }
}
$.extend(domOperate);
$.each(domOperate, function (k, v) {
    //k是函数名,v是对应的函数体,由于要多次遍历,声明一个变量接收v
    var va = v;
    $.prototype[k] = function (selector) {
        //定义一个空数组,用来接收每次添加的元素
        var tmp = [];
        var obj = this.constructor(selector);
        //对当前对象进行遍历,每一项都添加到目标对象中
        this.each(function (i, v) {
            //定义一个变量用于接收当前遍历的值
            var value = v;
            //对目标对象进行遍历,让目标对象的每一项都被添加元素
            $.each(obj, function (i, v) {
                //append具有剪切性,所以前面所有项没用克隆的节点添加,第length-1项即最后一项不再克隆
                //当目标对象和当前对象都是最后一项的时候才不克隆
                var val = i == obj.length - 1 ? value : value.cloneNode(true);
                //调用函数名对应的函数体,即调用相应的方法
                va(val, v);
                //把添加的元素追加到数组中
                tmp.push(val)
            })
        })


      return  this.pushStack(tmp);
        ////创建一个空的$对象
        //var obj = this.constructor();
        //// 把这个数组追加到这个$对象中
        //[].push.apply(obj, tmp);
        ////返回$对象,实现链式编程
        //return obj;
    }
})
//链恢复
$.prototype.extend({
    end: function () {
        return this.prevObject ? this.prevObject : this.constructor();
    }
})

//遍历上面一组方法对应的方法,把与其对应的方法转换一下参数位置即可使用相反的方法
$.each({
    append: 'appendTo',
    prepend: 'prependTo',
    before: 'insertBefore',
    after: 'insertAfter'
}, function (k, v) {
    $.prototype[k] = function (selector) {
        this.constructor(selector)[v](this);
        return this;
    }
})
//获取dom元素的方法
var domGet = {
    next: function (node) {
        var tmp = node;
        while ( tmp = tmp.nextSibling ) {
            if ( tmp.nodeType == 1 ) {
                return tmp;
            }
        }
        return null;
    },
    prev: function (node) {
        var tmp = node;
        while ( tmp = tmp.previousSibling ) {
            if ( tmp.nodeType == 1 ) {
                return tmp;
            }
        }
        return null;
    },
    nextAll: function (node) {
        var tmp = node,
            arr = [];
        while ( tmp = tmp.nextSibling ) {
            if ( tmp.nodeType == 1 ) {
                arr.push( tmp );
            }
        }
        return arr;
    },
    prevAll: function (node) {
        var tmp = node,
            arr = [];
        while ( tmp = tmp.previousSibling ) {
            if ( tmp.nodeType == 1 ) {
                arr.push( tmp );
            }
        }
        return arr;
    },
    parent: function (node) {
        return node.parentNode;
    }
}
$.each(domGet, function (key, value) {
    $.prototype[key] = function () {
        return this.pushStack($.unique(this.map(function (v) {
            return value(v);
        })))
    }
})

//添加事件
$.prototype.on= function (type, callback) {
    return this.each(function () {
        this.addEventListener(type,callback);
    })
}
$.each( ("blur focus focusin focusout load resize scroll unload click dblclick " +
"mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave " +
"change select submit keydown keypress keyup error contextmenu").split(" "), function ( i, v ) {

    $.prototype[ v ] = function ( callback ) {
        return this.on( v, callback );
    };
});

//class方法
$.prototype.extend({
    hasClass: function (className) {
        var flag=false;
        this.each(function () {
            if(this.className.split(" ").indexOf(className)!=-1){
               flag=true;
            }
        });
        return flag;
    },
    addClass: function (className) {
        return this.each(function () {
            this.classList.add(className)
        })
    },
    removeClass: function (className) {
        return this.each(function () {
            this.classList.remove(className)
        })
    },
    toggleClass: function (className) {
        return this.each(function () {
            this.classList.toggle(className)
        })
    }
})